package com.spzx.product;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.api.model.ProductSku;
import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.service.IProductSkuService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * 商品模块
 *
 * @author spzx
 */
@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
@Slf4j
public class SpzxProductApplication implements CommandLineRunner, ApplicationRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  系统模块启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }

    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        log.info("CommandLineRunner......");
        log.info("项目启动，将上架商品加入到Redis中位图中，全量同步");
        //1.查询现有上架商品ID，加入到redis中bitmap位图中
        List<ProductSku> productSkuList = productSkuService.list(
                new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getStatus, 1)
                        .select(ProductSku::getId)
        );
        //2.获取商品SKUID
        String key = "product:sku:data";
        productSkuList.forEach(a -> {
            redisTemplate.opsForValue().setBit(key, a.getId(), true);
        });
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("ApplicationRunner......");
    }

    @PostConstruct
    public void init() {
        log.info("init......");
    }
}